<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/7/15-9:29
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Module.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     *
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */

    namespace app\admin\controller;

    use http\Params;
    use service\DatasService;
    use service\UtilService;
    use think\Db;
    use think\Exception;


    class Module extends SystemBase
    {
        protected $API;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
           $this->API= new DatasService();
        }



        /**
         * @return mixed|void
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: newslist
         * @describe:新闻数据
         */
        public function newslist(){
            if($this->request->isAjax()) {
                $where = [];
                $param=$this->request->param();

                //菜单名称
                if(!empty($param['title'])) {
                    $title=$param['title'];
                    $where['title|author_name'] = ['like', "%$title%"];
                }

                if(!empty($param['cate'])){
                    $where['category'] =$param['cate'];
                }


                return GetPageData($where, "news", "date asc");
            }
            return $this->fetch();
        }



        /**
         * @return mixed|void
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: duan_list
         * @describe: 段子手视频
         */
        public function duan_video(){
            if($this->request->isAjax()) {
                $where = [];
                $param=$this->request->param();

                //菜单名称
                if(!empty($param['name'])) {
                    $title=$param['name'];
                    $where['text|name'] = ['like', "%$title%"];
                }

                return GetPageData($where, "duanjie", "passtime asc");
            }
            return $this->fetch();
        }


        /**
         * @return mixed|void
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: duan_img
         * @describe:段子趣图
         */
        public function duan_img(){
            if($this->request->isAjax()) {
                $where = [];
                $param=$this->request->param();

                //菜单名称
                if(!empty($param['name'])) {
                    $title=$param['name'];
                    $where['text|username'] = ['like', "%$title%"];
                }

                if(!empty($param['cate'])){
                    $where['type'] =$param['cate'];
                }

                return GetPageData($where, "jokeimg", "passtime asc");
            }
            return $this->fetch();
        }


        /**
         * @return mixed|void
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: friendLink
         * @describe:友链
         */
        public function friendLink(){
            if($this->request->isAjax()) {
                $where = [];
                $param=$this->request->param();
                //菜单名称
                if(!empty($param['name'])) {
                    $title=$param['name'];
                    $where['name'] = ['like', "%$title%"];
                }


                return GetPageData([], "friendlink");
            }
            return $this->fetch();
        }



        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: addLink
         * @describe:编辑添加友链
         */
        public function addLink(){
            if($this->request->isAjax()){
               $data=$this->request->post();
               try{
                   if(empty($data['id'])){ //添加
                       $data['admin_id']=self::$admin_info['id'];
                       $data['create_time']=time();
                       $data['ip']=UtilService::getip();
                       $data['time_formt']=date("Y-m-d H:i:s",time());
                       $res=\db("friendlink")->insert($data);
                       $msg="新增友链【".$data['name']."】";
                   }else{
                       //dump($data);die;
                       $data['update_time']=time();
                       $res=\db("friendlink")->where("id",intval($data['id']))->update($data);
                       $msg="修改友链【".$data['name']."】";
                   }
                   if($res){
                       $this->AddLogs($msg);

                       $this->success("操作成功");
                   }else{
                       $this->error("操作失败");
                   }

               }catch (Exception $exception){
                   $this->error($exception->getMessage());
               }

            }
            $id=$this->request->param("id/d");
            $this->assign("id",$id);
            if(isset($id)){
                $info=\db("friendlink")->find($id);
                $this->assign("info",$info);
            }

            return $this->fetch();
        }


        /**
         * @throws Exception
         * @throws \think\exception\PDOException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: delete_link
         * @describe:删除友链
         */
        public function delete_link(){
            if($this->request->isAjax()){
                $ids=$this->request->post("ids");
                $res=\db("friendlink")->where(["id"=>["in",$ids]])->delete();
                //dump($res);
                if($res){
                    $this->AddLogs('删除友链ID【'.$ids.'】');

                    $this->success("删除成功");
                }else{
                    $this->error("删除成功");
                }
            }
        }



        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: delete_video
         * @describe:删除段子视频数据
         */
        public function delete_video(){
            if($this->request->isAjax()){
                $ids=$this->request->post("ids");
                $res=\db("duanjie")->where(["id"=>["in",$ids]])->delete();
                //dump($res);
                if($res){
                    $this->AddLogs('删除段子视频数据ID【'.$ids.'】');

                    $this->success("删除成功");
                }else{
                    $this->error("删除成功");
                }
            }
        }


        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: delete_img
         * @describe:删除趣图
         */
        public function delete_img(){
            if($this->request->isAjax()){
                $ids=$this->request->post("ids");
                $res=\db("jokeimg")->where(["id"=>["in",$ids]])->delete();
                //dump($res);
                if($res){
                    $this->AddLogs('删除段子趣图或者文字数据ID【'.$ids.'】');

                    $this->success("删除成功");
                }else{
                    $this->error("删除成功");
                }
            }
        }



        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: delete_news
         * @describe:删除新闻
         */
        public function delete_news(){
            if($this->request->isAjax()){
                $ids=$this->request->post("ids");
                $res=\db("news")->where(["id"=>["in",$ids]])->delete();
                //dump($res);
                if($res){
                    $this->AddLogs('删除新闻数据ID【'.$ids.'】');

                    $this->success("删除成功");
                }else{
                    $this->error("删除成功");
                }
            }
        }

        /**
         * @return mixed
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: music
         * @describe:luckymusic
         */
        public function music(){

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: duanzi
         * @describe:数据采集
         */
        public function duanzi(){
            //dump($this->API->getnews()); die;
            return $this->fetch();
        }


        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: GetNews
         * @describe:获取新闻
         */
        public function GetNews(){
            /**类型,,top(头条，默认),shehui(社会),guonei(国内),shishang(时尚)
             * guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),
             **/
            if($this->request->isAjax()){

                $this->AddLogs('采集新闻数据');

                $arr=['top','shehui','guonei','shishang','guoji','yule','tiyu','junshi','keji','caijing'];
                foreach($arr as $v){
                    $res=$this->API->getnews($v);
                    if(isset($res['code'])&&$res['code']==0){
                        $this->error($res['msg']);
                        return ;
                    }

                    foreach($res as $k=>$f){
                        if(isset($k['thumbnail_pic_s02'])){
                            unset($k['thumbnail_pic_s02']);
                        }
                        if(isset($k['thumbnail_pic_s03'])){
                            unset($k['thumbnail_pic_s03']);
                        }
                        db("news")->insert($f);
                    }
                    //dump($res);

                }
                    $this->success("数据采集成功");

            }
        }

        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: GetWenZi
         * @describe:获取段子文字
         */
        public function GetWenZi(){
            if($this->request->isAjax()){

                $this->AddLogs('采集段子数据');

                $j=0;
                $arr=[];
                for($i=1;$i<11;$i++){
                    $res=$this->API->getwenzi($i);
                    if(isset($res['code'])&&$res['code']==0){
                        $this->error($res['msg']);
                        return ;
                    }
                    $ok=db("jokeimg")->insertAll($res);
                    if($ok){
                        $j++;
                    }else{
                        array_push($arr,$i);
                    }
                }
                if($j==10){
                    $this->success("10页数据采集成功");
                }else{
                    $str=@implode(",",$arr);
                    $this->error("第【".$str."】页数据采集失败");
                }
            }
        }

        /**
         * @throws \Exception
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: GetQuImg
         * @describe:获取趣图 pic/gif
         */
        public function GetQuImg(){
            if($this->request->isAjax()){

                $this->AddLogs('采集趣图');

                $j=0;
                $arr=[];
                for($i=1;$i<11;$i++){
                    $rand=rand(1,2);
                    $res=$this->API->getQuPic($i,$rand);
                    if(isset($res['code'])&&$res['code']==0){
                        $this->error($res['msg']);
                        return ;
                    }
                    $ok=db("jokeimg")->insertAll($res);
                    if($ok){
                        $j++;
                    }else{
                        array_push($arr,$i);
                    }
                }
                if($j==10){
                    $this->success("10页数据采集成功");
                }else{
                    $str=@implode(",",$arr);
                    $this->error("第【".$str."】页数据采集失败");
                }
            }
        }

        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: GetDuanZiData
         * @describe:采集段子手视频数据
         */
        public function GetDuanZiData(){
            if($this->request->isAjax()){
                $j=0;
                $arr=[];
                for($i=1;$i<11;$i++){
                    $res=$this->API->Getjoke($i);
                    if(isset($res['code'])&&$res['code']==0){
                        $this->error($res['msg']);
                        return ;
                    }
                    $ok=db("duanjie")->insertAll($res);
                    if($ok){
                       $j++;
                    }else{
                       array_push($arr,$i);
                    }
                }
                if($j==10){
                    $this->AddLogs('采集段子手视频数据');

                    $this->success("10页数据采集成功");
                }else{
                    $str=@implode(",",$arr);
                    $this->error("第【".$str."】页数据采集失败");
                }
            }
        }

        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: OptionDuan
         * @describe:优化段子数据表字段数据
         */
        public function OptionDuan(){
            if($this->request->isAjax()){

                $this->AddLogs('优化段子手数据表');
               Db::query("UPDATE `hhy_duanjie` SET video = REPLACE (video,'http://wvideo.spriteapp.cn','http://mvideo.spriteapp.cn')");
               $this->success("优化成功");
            }
        }






    }